package com.nuoniu.sibanyun.controller.crm;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmWebConfig;
import com.nuoniu.sibanyun.entity.crm.CrmWebImg;
import com.nuoniu.sibanyun.service.ISysCompanyService;
import com.nuoniu.sibanyun.service.crm.ICrmWebConfigService;
import com.nuoniu.sibanyun.service.crm.ICrmWebImgService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网站配置
 * @auther 小啊刚
 * @date 2020-07-25
 */
@RestController
@RequestMapping("crm/webConfig")
@Api(tags = "网站配置")
public class CrmWebConfigController {

    @Autowired
    private ICrmWebConfigService configService;
    @Autowired
    private ICrmWebImgService webImgService;
    @Autowired
    private ISysCompanyService companyService;

    /**
     * 获取配置详情
     * @author 小啊刚
     * @date 2020-07-25
     */
    @GetMapping("detail")
    public HttpResult detail() {
        return HttpResult.ok(configService.getById(1));
    }

    /**
     * 保存
     * @author 小啊刚
     * @date 2020-07-25
     */
    @PostMapping("save")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult save(@RequestBody CrmWebConfig crmWebConfig) {
        CrmWebConfig config = configService.getById(1);
        if(config.getFreeUsers() != crmWebConfig.getFreeUsers()) { //修改所有租户的免费数
            companyService.updateFreeUsers(crmWebConfig.getFreeUsers());
        }
        configService.updateById(crmWebConfig);
        return HttpResult.ok();
    }

    /**
     * 保存图片
     * @author 小啊刚
     * @date 2020-07-25
     */
    @PostMapping("saveWebImage")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult saveImage(@RequestBody CrmWebImg webImg) {
        if(StringUtils.isNotBlank(webImg.getImgUrl())) {
            webImgService.save(webImg);
        }
        return HttpResult.ok();
    }

    /**
     * 删除图片
     * @author 小啊刚
     * @date 2020-07-27
     */
    @PostMapping("deleteImg")
    @PreAuthorize("'system' == authentication.principal ")
    public HttpResult disableImg(@RequestBody List<Long> ids) {
        webImgService.removeByIds(ids);
        return HttpResult.ok();
    }

    /**
     * 获取图片列表
     * @author 小啊刚
     * @date 2020-07-25
     */
    @GetMapping("webImageList")
    public HttpResult webImageList(Page page, CrmWebImg webImg) {
        return HttpResult.ok(webImgService.webImageList(page, webImg));
    }

    /**
     * 获取佣金配置
     * @author 小啊刚
     * @date 2020-08-31
     */
    @GetMapping("getCommissionConfig")
    @ApiOperation(value = "获取佣金配置。firstReward:自销提出比例;secondReward:直属上级佣金比例,thirdReward:间接上级佣金比例;fourthReward:三级上级佣金比例",tags = "APP_系统配置")
    public HttpResult getCommissionConfig() {
        Map data = new HashMap();
        CrmWebConfig config = configService.getById(1);
        data.put("firstReward", config.getFirstReward() * 0.01);
        data.put("secondReward", config.getSecondReward() * 0.01);
        data.put("thirdReward", config.getThirdReward() * 0.01);
        data.put("fourthReward", config.getFourthReward() * 0.01);
        return HttpResult.ok(data);
    }
}
